package simulator;

import java.awt.Image;
import java.io.File;
import java.util.ArrayList;

import javax.swing.ImageIcon;

public class ImagesSimulator extends javax.swing.JFrame {
	private static final long serialVersionUID = 1L;
	JPanelConFondo jPanelConFondo1 = new JPanelConFondo();
	JPanelConFondo jPanelConFondo2 = new JPanelConFondo();
	
	public ImagesSimulator() {
		initComponents();
	}

	public static void main(String[] args) {
		
		ImagesUtils utils = new ImagesUtils();
		
		boolean graphs = utils.getGraphs();
		boolean animation = utils.getAnimation();
		int iterations = 1;
		int limit = utils.getLimit();
		int netSize = utils.getNetSize();
		double alpha = utils.getAlpha();
		ArrayList<Topology> topologies = new ArrayList<Topology>();
		Topology topology = utils.getTopology();
		topologies.add(topology);
		Simulator simulator = new Simulator(graphs, animation, iterations, limit, netSize, topologies);
		simulator.utils.setStep(1);
		simulator.utils.setAlpha(alpha);
		simulator.utils.setInfectedPercentage(utils.getInfectedPercentage());
		simulator.utils.setT1mean(utils.getT1mean());
		simulator.utils.setT2mean(utils.getT2mean());
		simulator.utils.setT3mean(utils.getT3mean());
		simulator.utils.setT1std(utils.getT1std());
		simulator.utils.setT2std(utils.getT2std());
		simulator.utils.setT3std(utils.getT3std());
		simulator.simulate();
		
    	ImagesSimulator imgSimulator = new ImagesSimulator();
		imgSimulator.setLocationRelativeTo(null);
		imgSimulator.setVisible(true);
		imgSimulator.setSize(500,500);
		
		File refFile = new File(utils.getReferencesFile());
		Image reference = new ImageIcon(refFile.getAbsolutePath()).getImage();
		imgSimulator.jPanelConFondo1.setImagen(reference);
		Image image;
		int k = 0;
		String dirPath = utils.getGraphFolderPath() + "/"+topology.name()+"/alpha"+alpha+"/";
//		String dirPath = utils.getGraphFolderPath() + "/RING/alpha0.4/";
//		String dirPath = utils.getGraphFolderPath() + "/RANDOM/alpha0.3/";
		File dir = new File(dirPath);
		dir.mkdirs();
		int cantRestante = dir.list().length;
		do {
			imgSimulator.setTitle("Simulacion: "+k+" Hs." );
			File f = new File(dirPath + "graph" + k + ".jpg");
			if(f.exists()){
				image = new ImageIcon(f.getAbsolutePath()).getImage();
				imgSimulator.jPanelConFondo2.setImagen(image);
				cantRestante--;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			k++;
		} while (cantRestante > 0);
	}
	
	
	private void initComponents() {

        final javax.swing.JLabel lbExterna = new javax.swing.JLabel();
        final javax.swing.JLabel lbRef = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Simulacion: 0 Hs.");

        jPanelConFondo2.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));

        lbExterna.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lbExterna.setForeground(new java.awt.Color(255, 204, 102));
        lbExterna.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        

        javax.swing.GroupLayout jPanelConFondo1Layout = new javax.swing.GroupLayout(jPanelConFondo2);
        jPanelConFondo2.setLayout(jPanelConFondo1Layout);
        jPanelConFondo1Layout.setHorizontalGroup(
            jPanelConFondo1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 172, Short.MAX_VALUE)
            .addGroup(jPanelConFondo1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbExterna, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelConFondo1Layout.setVerticalGroup(
            jPanelConFondo1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 136, Short.MAX_VALUE)
            .addGroup(jPanelConFondo1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbExterna, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                .addContainerGap())
        );
        
        lbRef.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        lbRef.setForeground(new java.awt.Color(255, 204, 102));
        lbRef.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        
        javax.swing.GroupLayout jPanelConFondo2Layout = new javax.swing.GroupLayout(jPanelConFondo2);
        jPanelConFondo2.setLayout(jPanelConFondo2Layout);
        jPanelConFondo2Layout.setHorizontalGroup(
            jPanelConFondo2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 172, Short.MAX_VALUE)
            .addGroup(jPanelConFondo2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbRef, javax.swing.GroupLayout.DEFAULT_SIZE, 152, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanelConFondo2Layout.setVerticalGroup(
            jPanelConFondo2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 136, Short.MAX_VALUE)
            .addGroup(jPanelConFondo2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbRef, javax.swing.GroupLayout.DEFAULT_SIZE, 114, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelConFondo1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, 70)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jPanelConFondo2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(jPanelConFondo1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanelConFondo2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }

}
